Skip to main content

Type Conversion Tables in xtd

In this article

Widening conversion occurs when a value of one type is converted to another type that is of equal or greater size. A narrowing conversion occurs when a value of one type is converted to a value of another type that is of a smaller size. The tables in this topic illustrate the behaviors exhibited by both types of conversions.

Widening Conversions

The following table describes the widening conversions that can be performed without the loss of information.

TypeCan be converted without data loss to
byteuint16, int16, uint32, int32, uint64, int64, single, double, decimal
sbyteint16, int32, int64, single, double, decimal
int16int32, int64, single, double, decimal
uint16uint32, int32, uint64, int64, single, double, decimal
charuint16, uint32, int32, uint64, int64, single, double, decimal
int32int64, double, decimal
uint32int64, uint64, double, decimal
int64decimal
uint64decimal
singledouble

Some widening conversions to single or double can cause a loss of precision. The following table describes the widening conversions that sometimes result in a loss of information.

TypeCan be converted to
int32single
uint32single
int64single, double
uint64single, double
decimalsingle, double

Narrowing Conversions

A narrowing conversion to single or double can cause a loss of information. If the target type cannot properly express the magnitude of the source, the resulting type is set to the constant positive_infinity or negative_infinity. positive_infinity results from dividing a positive number by zero and is also returned when the value of a single or double exceeds the value of the max_value field. negative_infinity results from dividing a negative number by zero and is also returned when the value of a single or double falls below the value of the min_value field. A conversion from a double to a single might result in positive_infinity or negative_infinity. A narrowing conversion can also result in a loss of information for other data types. However, an overflow_exception is thrown if the value of a type that is being converted falls outside of the range specified by the target type's max_value and min_value fields, and the conversion is checked by the runtime to ensure that the value of the target type does not exceed its max_value or min_value. Conversions that are performed with the xtd::convert class are always checked in this manner. The following table lists conversions that throw an overflow_exception using xtd::convert or any checked conversion if the value of the type being converted is outside the defined range of the resulting type.

TypeCan be converted to
bytesbyte
sbytebyte, uint16, uint32, uint64
int16byte, sbyte, uint16
uint16byte, sbyte, int16
int32byte, sbyte, int16, uint16,uint32
uint32byte, sbyte, int16, uint16, int32
int64byte, sbyte, int16, uint16, int32,uint32,uint64
uint64byte, sbyte, int16, uint16, int32, uint32, int64
decimalbyte, sbyte, int16, uint16, int32, uint32, int64, uint64
singlebyte, sbyte, int16, uint16, int32, uint32, int64, uint64
doublebyte, sbyte, int16, uint16, int32, uint32, int64, uint64

See also